﻿/**
 * Created by Administrator on 2017/8/18 0018.
 */
var express = require('express');
var crypto = require("crypto");
var qr = require('qr-image')
var multer = require("multer");
var config = require('../../config.js');
var log = require('../../util/logger');
var db = require('../../db/db.js');
var moment = require("moment");

var router = express.Router();
var news_model = require('../../model/news.js');
var goods_model = require("../../model/yoshop_goods");
var yoshop_user_model = require("../../model/yoshop_user");


var myutil = require("../../util/util");






router.post('/list', async function (req, res) {
    var page;
    var rows;
    if (isNaN(req.body.page)) {
        page = 1;
    } else {
        page = +req.body.page;
    }
    if (isNaN(req.body.rows)) {
        rows = 10;
    } else {
        rows = +req.body.rows;
    }
    var first = rows * (page - 1);

    var where = " 1=1 ";
    if (typeof (req.body.name) != "undefined" && req.body.name != "") {
        where = where + " and exam.name like  '%" + req.body.name + "%'";

    }
    if (typeof (req.body.subject) != "undefined" && req.body.subject != "") {
        where = where + " and exam.subject='" + req.body.subject + "'";

    }
    if (typeof (req.body.gradeid) != "undefined" && req.body.gradeid != "") {
        where = where + " and exam.gradeid=" + req.body.gradeid;

    }
    if (typeof (req.body.schoolid) != "undefined" && req.body.schoolid != "") {
        where = where + " and exam.schoolid=" + req.body.schoolid;

    }



    console.log("where", where);
    var list = await news_model.getlist(rows, first, where, " join yoshop_user on yoshop_user.user_id=news.user_id ", " order by news.id desc", "news.*,yoshop_user.phone");
    for (var i = 0; i < list.length; i++) {

        list[i].createtime = moment(list[i].createtime).format("YYYY-MM-DD HH:mm:ss");

        var good = await goods_model.getinfo("id=" + list[i].goodid);
        if (good.length > 0) {
            list[i].goods_name = good[0].goods_name;
        }

    }
    var count = await news_model.getcount(where, " join yoshop_user on yoshop_user.user_id=news.user_id ");
    var allcount = parseInt(parseInt(count) / rows) + 1;
    if (count % rows == 0) {
        allcount = allcount - 1;
    }

    res.send({ status: 200, data: list, message: "", allcount: allcount, count: count });

});






router.post('/info', async function (req, res) {

    var where = " id=" + req.body.id;



    var list = await news_model.getinfo(where);


    if (list.length > 0) {
        var info = list[0];


        var yoshop_user = await yoshop_user_model.getinfo("user_id=" + info.user_id);
        if (yoshop_user.length > 0) {
            info.nickname = yoshop_user[0].nickname;
        }

        info.imglist = info.imglist.split(",");


        info.createtime = moment(info.createtime).format("YYYY-MM-DD HH:mm:ss");

        res.send({ status: 200, data: info });
    } else {
        res.send({ status: 400, data: "" });
    }




});

router.post("/edit", async function (req, res) {
    var shopconfig = myutil.getconfig();
    var data = req.body;
    var obj = await news_model.edit(data);
    if (obj.code == 200) {
        if (req.body.state == "1") {
            var jifen = shopconfig.newsokscore;
            db.execute("update yoshop_user set score=score+" + jifen + " where user_id=" + data.user_id, []);

        }

        var result = {
            "status": "200",
            "message": "success"
        }
        res.jsonp(result);
    } else {
        var result = {
            "status": "400",
            "message": "error"
        }
        res.jsonp(result);
    }

})

router.delete('/del', async function (req, res) {

    var where = " id=" + req.body.id;
    var obj = await news_model.del(where);
    if (obj.code == 200) {

        var result = {
            "status": "200",
            "message": "success"
        }
        res.jsonp(result);
    } else {
        var result = {
            "status": "400",
            "message": "success"
        }
        res.jsonp(result);
    }

});


module.exports = router;   /*暴露这个 router模块*/